package com.zeta.eduservice.controller.question;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.question.EduCourse;
import com.zeta.eduservice.entity.vo.CourseInfoForm;
import com.zeta.eduservice.entity.vo.CoursePublishVo;
import com.zeta.eduservice.entity.vo.CourseQuery;
import com.zeta.eduservice.service.EduCourseClassService;
import com.zeta.eduservice.service.EduCourseService;
import com.zeta.eduservice.service.EduVideoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-08-02
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {
	@Autowired
	private EduCourseService courseService;
	@Autowired
	private EduVideoService videoService;

	@ApiOperation(value = "新增课程")
	@PostMapping("")
	public R saveCourseInfo(
			@ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
			@RequestBody CourseInfoForm courseInfoForm){
		String courseId = courseService.saveCourseInfo(courseInfoForm);
		if(!StringUtils.isEmpty(courseId)){
			return R.ok().data("courseId", courseId);
		}else{
			return R.error().message("保存失败");
		}
	}
	@ApiOperation("获取课程信息")
	@GetMapping("{courseId}")
	public R getCourseById(@PathVariable String courseId) {
		
		CourseInfoForm form=courseService.getCourseInfo(courseId);
		
		return R.ok().data("CourseInfoForm",form);
	}
	@DeleteMapping("{id}")
	public R delete(@PathVariable String id) {
		EduCourse byId = courseService.getById(id);
	//	videoService.removeVideoByCourseId(byId.getId());
		boolean flag = courseService.removeById1(id);
		if(flag==true){
	        return R.ok();    
	    }else{
	        return R.error().message("删除失败");
	    }
	}
	@ApiOperation("修改")
	@PutMapping("{id}")
	public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm,@PathVariable String id) {
		courseInfoForm.setId(id);
	
		courseService.updateCourseForm(courseInfoForm);
		return R.ok();
	}
	

	@ApiOperation(value = "根据ID获取课程发布信息")
	@GetMapping("course_publish_info/{id}")
	public R getCoursePublishVoById(
	    @ApiParam(name = "id", value = "课程ID", required = true)
	    @PathVariable String id){

	    CoursePublishVo courseInfoForm = courseService.getCoursePublishVoById(id);
	    return R.ok().data("item", courseInfoForm);
	}
	@ApiOperation("课程发布")
	@PostMapping("publish/{id}")
	public R publish(@PathVariable String id) {
		EduCourse eduCourse = new EduCourse();
		eduCourse.setId(id);
		eduCourse.setStatus("Normal");
		courseService.updateById(eduCourse);	
		return R.ok();
	}
	@ApiOperation(value = "分页课程列表")
	@GetMapping("{page}/{limit}")
	public R pageQuery(
			@ApiParam(name = "page", value = "当前页码", required = true)
			@PathVariable Long page,

			@ApiParam(name = "limit", value = "每页记录数", required = true)
			@PathVariable Long limit,

			@ApiParam(name = "courseQuery", value = "查询对象", required = false)
					CourseQuery courseQuery){

		Page<EduCourse> pageParam = new Page<>(page, limit);

		courseService.pageQuery(pageParam, courseQuery);
		List<EduCourse> records = pageParam.getRecords();

		long total = pageParam.getTotal();

		return  R.ok().data("total", total).data("rows", records);
	}
}

